package com.core.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class FileUtil {

    /**
     * 读取文件转为字符串
     * @param multipartFile
     * @return
     */
    public static String MultipartFileToString(MultipartFile multipartFile) {
        if (multipartFile != null) {
            String result = null;
            try {
                Reader reader = new InputStreamReader(multipartFile.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(reader);
                StringBuilder stringBuilder = new StringBuilder();
                String lineStr;

                // 逐行读取
                while (null != (lineStr = bufferedReader.readLine())) {
                    stringBuilder.append(lineStr).append("\n");
                }
                result = stringBuilder.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return result;
        } else {
            return null;
        }
    }
}
